/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.order.poa.service.spring;

import java.text.SimpleDateFormat;
import java.util.*;

import com.activiti.dao.model.Activiti_SubTaskInfo;
import com.activiti.dao.model.Activiti_TaskInfo;
import com.activiti.dao.service.ActivitiService;
import com.activiti.dao.service.TaskSubService;
import com.erp.common.voucher.util.FinVoucherUtil;
import com.erp.hr.dao.model.HrStaffInfoRO;
import com.erp.masterdata.vendor.dao.model.MdVendor;
import com.erp.masterdata.vendor.dao.model.MdVendorCO;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.redragon.framework.hibernate.model.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erp.order.poa.dao.PoAgreementHeadDao;
import com.erp.order.poa.dao.PoAgreementLineDao;
import com.erp.order.poa.dao.model.PoAgreementHead;
import com.erp.order.poa.dao.model.PoAgreementHeadCO;
import com.erp.order.poa.service.PoAgreementHeadService;
import com.erp.order.poa.service.PoAgreementLineService;

@Service
@Transactional(rollbackFor=Exception.class)
public class PoAgreementHeadServiceImpl implements PoAgreementHeadService {

    //注入Dao
    @Autowired
    private PoAgreementHeadDao poAgreementHeadDao;
    @Autowired
    private ActivitiService activitiService;
    @Autowired
    private TaskSubService taskSubService;
    
    @Override
    public void insertDataObject(PoAgreementHead obj) {
        this.poAgreementHeadDao.insertDataObject(obj);
    }

    @Override
    public void updateDataObject(PoAgreementHead obj) {
        this.poAgreementHeadDao.updateDataObject(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(PoAgreementHead obj) {
        this.poAgreementHeadDao.insertOrUpdateDataObject(obj);
    }

    @Override
    public void deleteDataObject(PoAgreementHead obj) {
        this.poAgreementHeadDao.deleteDataObject(obj);
        //this.poAgreementLineService.deletetPoLineByPoHeadCode(obj.getPoHeadCode());
    }

    @Override
    public List<PoAgreementHead> getDataObjects() {
        return this.poAgreementHeadDao.getDataObjects();
    }

    @Override
    public PoAgreementHead getDataObject(int id) {
        return this.poAgreementHeadDao.getDataObject(id);
    }

    @Override
    public PoAgreementHead getDataObject(String code) {
        return this.poAgreementHeadDao.getDataObject(code);
    }

    @Override
    public List<PoAgreementHead> getDataObjects(PoAgreementHeadCO paramObj) {
        return this.poAgreementHeadDao.getDataObjects(paramObj);
    }

    @Override
    public List<PoAgreementHead> getDataObjects(Pages pages) {
        return this.poAgreementHeadDao.getDataObjects(pages);
    }
    
    @Override
    public List<PoAgreementHead> getDataObjects(Pages pages, PoAgreementHeadCO paramObj) {
        return this.poAgreementHeadDao.getDataObjects(pages, paramObj);
    }
    
    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, PoAgreementHeadCO paramObj) {
        return this.poAgreementHeadDao.getDataObjectsArray(pages, paramObj);
    }
    
    @Override
    public List<PoAgreementHead> getDataObjectsForDataAuth(String dataAuthSQL, Pages pages, PoAgreementHeadCO paramObj) {
        return this.poAgreementHeadDao.getDataObjectsForDataAuth(dataAuthSQL, pages, paramObj);
    }
    
    @Override
    public void updateApproveStatus(String code, String approveStatus) {
        this.poAgreementHeadDao.updateApproveStatus(code, approveStatus);
        //更新行表版本号
        if(approveStatus.equals("UNSUBMIT")) {
            //this.poAgreementLineDao.updateLineForVersion(code);
        }
    }

    @Override
    public void updateStatus(String code, String status) {
        this.poAgreementHeadDao.updateStatus(code, status);
    }

    @Override
    public String getCodeAutomatic(String orgcode) {
        Calendar    rightNow  =    Calendar.getInstance();
        Integer year = rightNow.get(Calendar.YEAR);
        StringBuffer code = new StringBuffer("JNFW"+year+orgcode+"-");
        List<PoAgreementHead> list = this.poAgreementHeadDao.getDataObjects();
        if (list.size()>0){
            FinVoucherUtil.incrVoucherNumberCache("HT_CODE");
            Long numberCache = FinVoucherUtil.getVoucherNumberCache("HT_CODE");
            String seqNum = String.format("%04d", numberCache);
            code.append(seqNum);
        }else{
            //初始化Redis
            FinVoucherUtil.setVoucherNumberCache("HT_CODE",0l);
            FinVoucherUtil.incrVoucherNumberCache("HT_CODE");
            Long numberCache = FinVoucherUtil.getVoucherNumberCache("HT_CODE");
            String seqNum = String.format("%04d", numberCache);
            code.append(seqNum);
        }
        return code.toString();
    }

    @Override
    public void saveActivitiTaskInfo(String bussname, HrStaffInfoRO staffInfo, Task task, ProcessInstance processInstance, String status) {
        //保存审批意见(主表)
        Activiti_TaskInfo Activiti_TaskInfo = new Activiti_TaskInfo();
        Activiti_TaskInfo.setCompany_code(staffInfo.getCompanyCode());
        Activiti_TaskInfo.setTask_id(Integer.parseInt(task.getId()));
        Activiti_TaskInfo.setDepartment_code(staffInfo.getDepartmentCode());
        Activiti_TaskInfo.setProc_name(bussname);
        Activiti_TaskInfo.setProc_inst_id(processInstance.getProcessInstanceId());
        Activiti_TaskInfo.setStaff_code(staffInfo.getStaffCode());
        Activiti_TaskInfo.setStatus(status);//待审批
        Activiti_TaskInfo.setCreate_time(new Date());
        activitiService.insertOrUpdateDataObject(Activiti_TaskInfo);
    }

    @Override
    public void saveSubActivitiTaskInfo(HrStaffInfoRO staffInfo, Task task, ProcessInstance processInstance, String status, String substatus, String subcontent) {
        Activiti_TaskInfo activiti_taskInfo = activitiService.queryActivitiTaskInfoByPid(processInstance.getProcessInstanceId());
        activiti_taskInfo.setStatus(status);
        activitiService.updateDataObject(activiti_taskInfo);
        //保存审批意见(子表)
        Activiti_SubTaskInfo activiti_subTaskInfo = new Activiti_SubTaskInfo();
        activiti_subTaskInfo.setTask_id(Integer.parseInt(processInstance.getProcessInstanceId()));
        activiti_subTaskInfo.setCompany_code(staffInfo.getCompanyCode());
        activiti_subTaskInfo.setCreate_time(new Date());
        activiti_subTaskInfo.setTask_sub_id(Integer.parseInt(task.getId()));
        activiti_subTaskInfo.setTask_sub_name(task.getName());
        activiti_subTaskInfo.setDepartment_code(staffInfo.getDepartmentCode());
        activiti_subTaskInfo.setStatus(substatus);//审批结果
        activiti_subTaskInfo.setProc_contents(subcontent);//审批意见
        activiti_subTaskInfo.setStaff_code(staffInfo.getStaffCode());
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        activiti_subTaskInfo.setProc_time(dateString);
        taskSubService.insertOrUpdateDataObject(activiti_subTaskInfo);
    }

    @Override
    public Map<String, String> getPoAgreementHeadMap() {
        Map<String, String> map = new HashMap<String, String>();
        PoAgreementHeadCO poAgreementHeadCO = new PoAgreementHeadCO();
        poAgreementHeadCO.setStatus("CONFIRM");
        poAgreementHeadCO.setApproveStatus("COMPLETE");
        List<PoAgreementHead> list = this.poAgreementHeadDao.getDataObjects(poAgreementHeadCO);
        for(PoAgreementHead poAgreementHead: list) {
            map.put(poAgreementHead.getPoHeadCode(), poAgreementHead.getPoName());
        }
        return map;
    }

}